package com.lune.job.controller;


import com.lune.job.pojo.Dict;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/dict")
@Api(tags = "字典管理")
public class DictController {

    @Autowired
    private IDictService dictService;

    @ApiOperation(value = "获取所有字典")
    @GetMapping("/")
    public List<Dict> getAllDict() {
        return dictService.list();
    }

    @ApiOperation(value = "添加字典")
    @PostMapping("/")
    public RespBean create(@RequestBody Dict dict) {
        if (dictService.save(dict)) {
            return RespBean.success("添加字典成功");
        } else {
            return RespBean.error("添加字典失败");
        }
    }

    @ApiOperation(value = "删除单个字典")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        if (dictService.removeById(id)) {
            return RespBean.success("删除字典成功");
        }
        return RespBean.error("删除字典失败");
    }

    @ApiOperation(value = "批量删除字典")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (dictService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除字典成功");
        } else {
            return RespBean.error("批量删除字典失败");
        }
    }

    @ApiOperation(value = "修改字典")
    @PutMapping("/")
    public RespBean update(@RequestBody Dict dict) {
        if (dictService.updateById(dict)) {
            return RespBean.success("修改字典成功");
        } else {
            return RespBean.error("修改字典失败");
        }
    }

    @ApiOperation(value = "根据id查询字典")
    @GetMapping("/detail")
    public Dict detail(Integer id) {
        return dictService.getById(id);
    }

    @ApiOperation(value = "分页查询字典")
    @PostMapping("/page")
    public ResPageBean getAllDictByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        @RequestBody Dict dict) {
        return dictService.getAllDictByPage(currentPage, size, dict);
    }

    @ApiOperation(value = "根据dict查询字典")
    @PostMapping("/query")
    public List<Dict> queryDict(
             Dict dict) {
        return dictService.queryDict(dict);
    }
}
